package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;

import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Diamond
 * @time 2019/11/13/20:27
 */
@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;

    @Override
    public void add(Role role, Integer[] permissionIds, Integer[] menuIds) {
        //1.保存检查组数据
        roleDao.add(role);


        //2.获取检查组Id
        Integer roleId = role.getId();


        //3.循环存进去
        setRoleAndPermission(roleId,permissionIds);

        setRoleAndMenu(roleId,menuIds);
    }

    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);
        Page<Role> page = roleDao.selectByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public Role findById(Integer id) {
        return roleDao.findById(id);
    }

    @Override
    public List<Integer> findPermissionIdsByRoleId(Integer id) {
        return roleDao.findPermissionIdsByRoleId(id);
    }

    @Override
    public List<Integer> findMenuIdsByRoleId(Integer id) {
        return roleDao.findMenuIdsByRoleId(id);
    }

    @Override
    public void edit(Role role, Integer[] permissionIds, Integer[] menuIds) {
        //根据检查组id删除中间表数据（清理原有关联关系）
        roleDao.deleteAssociation1(role.getId());
        roleDao.deleteAssociation2(role.getId());
        //向中间表加
        setRoleAndPermission(role.getId(),permissionIds);
        setRoleAndMenu(role.getId(),menuIds);
        //更新检查组基本信息
        roleDao.edit(role);
    }

    @Override
    public void delete(Integer id) {
        //1.删除检查组与检查项的中间表中的数据
        roleDao.deleteAssociation1(id);
        roleDao.deleteAssociation2(id);
        //2.删除检查组
        roleDao.delete(id);
    }

    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }

    public void setRoleAndPermission(Integer roleId,Integer[] permissionIds){
        if (permissionIds != null && permissionIds.length > 0){
            for (Integer permissionId : permissionIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("roleId",roleId);
                map.put("permissionId",permissionId);
                roleDao.setRoleAndPermission(map);
            }
        }
    }
    public void setRoleAndMenu(Integer roleId,Integer[] menuIds){
        if (menuIds != null && menuIds.length > 0){
            for (Integer menuId : menuIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("roleId",roleId);
                map.put("menuId",menuId);
                roleDao.setRoleAndMenu(map);
            }
        }
    }
}
